﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BullShitPokerServer
{
    class Worker
    {
        static string[] moves = {"One Card", "Pair", "Two Pairs", 
                "Set", "Flash", "Street", "Full House (3 and 2)", "Square", "Street Flash",
                "Royal Flash", "Five Cards", "Six Cards", "Seven Cards", "Eight Cards"};
        static string[] cards = { "2", "3", "4", "5", "6", "7", "8", "9", 
                                    "10", "Jack", "Queen", "King", "Ace" };
        static string[] types = { "Diamonds", "Crosses", "Hearts", "Spades" };

        public static string Move(string input)
        {
            string chat = "";
            string[] data = input.Split('|');
            int type = Int32.Parse(data[1]);
            int card = Int32.Parse(data[2]);
            int card2 = Int32.Parse(data[3]);
            #region
            switch (type)
            {
                case 0:
                    chat += moves[type] + " of " + cards[card];
                    break;
                case 1:
                    goto case 0;
                case 2:
                    chat += moves[type] + " of " + cards[card] + " and " + cards[card2];
                    break;
                case 3:
                    chat += moves[type] + " of " + cards[card];
                    break;
                case 4:
                    chat += moves[type] + " of " + types[card2];
                    break;
                case 5:
                    chat += moves[type] + " to " + cards[card];
                    break;
                case 6:
                    chat += moves[type] + " of " + cards[card] + " and " + cards[card2];
                    break;
                case 7:
                    goto case 0;
                case 8:
                    chat += moves[type] + " to " + cards[card] + " of " + types[card2];
                    break;
                case 9:
                    chat += moves[type] + " of " + cards[card2];
                    break;
                case 10:
                    goto case 0;
                case 11:
                    goto case 0;
                case 12:
                    goto case 0;
                case 13:
                    goto case 0;
            }
            #endregion
            return chat;
        }

        public static bool oneCard(string allCards, string card)
        {
            string[] data = allCards.Split('|');
            foreach (string s in data)
            {
                if (s.Split(',')[0] == card || s.Split(',')[0] == "0") return true;
            }
            return false;
        }

        public static bool onePair(string allCards, string card)
        {
            string[] data = allCards.Split('|');
            bool already = false;
            foreach (string s in data)
            {
                if ((s.Split(',')[0] == card  || s.Split(',')[0] == "0")
                    && already) return true;
                if ((s.Split(',')[0] == card || s.Split(',')[0] == "0")
                    && !already) already = true;
            }
            return false;
        }

        public static bool twoPair(string allCards, string card1, string card2)
        {
            string[] data = allCards.Split('|');
            bool already1 = false;
            bool already2 = false;
            List<string> cards = new List<string>();
            foreach (string s in data)
            {
                if ((s.Split(',')[0] == card1 || s.Split(',')[0] == "0")
                    && !already1 && !cards.Contains(s))
                {
                    already1 = true;
                    cards.Add(s);
                }
                else if ((s.Split(',')[0] == card2 || s.Split(',')[0] == "0")
                    && !already2 && !cards.Contains(s))
                {
                    already2 = true;
                    cards.Add(s);
                }
                if (already1 && already2) return true;
            }
            return false;
        }

        public static bool setCard(string allCards, string card1)
        {
            string[] data = allCards.Split('|');
            byte already = 0;
            foreach (string s in data)
            {
                if (s.Split(',')[0] == card1 || s.Split(',')[0] == "0") already++;
                if (already >= 3) return true;
            }
            return false;
        }

        public static bool Flash(string allCards, string card1)
        {
            string[] data = allCards.Split('|');
            byte already = 0;
            foreach (string s in data)
            {
                if (s.Split(',')[1] == card1 || s.Split(',')[0] == "0") already++;
                if (already >= 5) return true;
            }
            return false;
        }

        public static bool Street(string allCards, string card1)
        {
            string[] data = allCards.Split('|');
            int max = Int32.Parse(card1);
            int min = max - 4;
            bool?[] already = new bool?[5];
            foreach (string s in data)
            {
                int card = Int32.Parse(s.Split(',')[0]);
                if (card >= min && card <= max)
                    already[max - card] = true;
            }
            foreach (string s in data)
            {
                int card = Int32.Parse(s.Split(',')[0]);
                if (card == 0)
                {
                    for (int i = 0; i < 5; i++)
                    {
                        if (already[i] != true)
                        {
                            already[i] = true;
                            break;
                        }
                    }
                }
            }
            for (int i = 0; i < 5; i++)
            {
                if (already[i] != true)
                {
                    return false;
                }
            }
            return true;
        }

        public static bool FullHouse(string allCards, string card1, string card2)
        {
            string[] data = allCards.Split('|');
            byte already1 = 0;
            byte already2 = 0;
            List<string> cards = new List<string>();
            foreach (string s in data)
            {
                if ((s.Split(',')[0] == card1 || s.Split(',')[0] == "0")
                     && already1 < 3 && !cards.Contains(s))
                {
                    already1++;
                    cards.Add(s);
                }
                else if ((s.Split(',')[0] == card1 || s.Split(',')[0] == "0")
                    && already2 < 2 && !cards.Contains(s))
                {
                    already2++;
                    cards.Add(s);
                }
                if (already1 >= 3 && already2 >= 2) return true;
            }
            return false;
        }

        public static bool fourCard(string allCards, string card1)
        {
            string[] data = allCards.Split('|');
            byte already = 0;
            foreach (string s in data)
            {
                if (s.Split(',')[0] == card1 || s.Split(',')[0] == "0") already++;
                if (already >= 4) return true;
            }
            return false;
        }

        public static bool fiveCard(string allCards, string card1)
        {
            string[] data = allCards.Split('|');
            byte already = 0;
            foreach (string s in data)
            {
                if (s.Split(',')[0] == card1 || s.Split(',')[0] == "0") already++;
                if (already >= 5) return true;
            }
            return false;
        }

        public static bool sixCard(string allCards, string card1)
        {
            string[] data = allCards.Split('|');
            byte already = 0;
            foreach (string s in data)
            {
                if (s.Split(',')[0] == card1 || s.Split(',')[0] == "0") already++;
                if (already >= 6) return true;
            }
            return false;
        }

        public static bool sevenCard(string allCards, string card1)
        {
            string[] data = allCards.Split('|');
            byte already = 0;
            foreach (string s in data)
            {
                if (s.Split(',')[0] == card1 || s.Split(',')[0] == "0") already++;
                if (already >= 7) return true;
            }
            return false;
        }

        public static bool eightCard(string allCards, string card1)
        {
            string[] data = allCards.Split('|');
            byte already = 0;
            foreach (string s in data)
            {
                if (s.Split(',')[0] == card1 || s.Split(',')[0] == "0") already++;
                if (already >= 8) return true;
            }
            return false;
        }

        public static bool StreetFlash(string allCards, string card1, string card2)
        {
            string[] data = allCards.Split('|');
            int max = Int32.Parse(card1);
            int min = max - 4;
            bool?[] already = new bool?[5];
            foreach (string s in data)
            {
                int card = Int32.Parse(s.Split(',')[0]);
                if ((card >= min && card <= max) && s.Split(',')[1] == card2)
                    already[max - card] = true;
            }
            foreach (string s in data)
            {
                int card = Int32.Parse(s.Split(',')[0]);
                if (card == 0)
                {
                    for (int i = 0; i < 5; i++)
                    {
                        if (already[i] != true)
                        {
                            already[i] = true;
                            break;
                        }
                    }
                }
            }
            for (int i = 0; i < 5; i++)
            {
                if (already[i] != true)
                {
                    return false;
                }
            }
            return true;
        }

        public static bool RoyalFlash(string allCards,string card2)
        {            
            return StreetFlash(allCards, "12", card2);
        }

    }
}
